package router

import (
	"fmt"

	"pt-gin-mongo/controller"
	"pt-gin-mongo/middleware"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

func InitRouter() *gin.Engine {
	gin.SetMode("debug")
	router := gin.New()

	// 404
	router.NoRoute(func(c *gin.Context) {
		path := c.Request.URL.Path
		method := c.Request.Method
		c.String(404, fmt.Sprintf("%s %s not found", method, path))
	})

	router.Use(
		cors.Default(),
		middleware.ResponseFormat(),
	)

	group := router.Group("/api")

	// open
	controller.InitOpen(group)

	return router
}
